package com.sicilin.system.user.service;

import com.sicilin.common.exception.BusinessException;
import com.sicilin.common.exception.ServiceException;
import com.sicilin.dao.AuthDao;
import com.sicilin.dao.RoleDao;
import com.sicilin.dao.UserDao;
import com.sicilin.po.TSysRole;
import com.sicilin.po.TSysUserRole;
import com.sicilin.system.role.dto.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional(readOnly = true,propagation = Propagation.REQUIRED)
public class UserRoleService {

    @Autowired(required = false)
    private UserDao userDao;

    @Autowired(required = false)
    private RoleDao roleDao;

    public List<Role> getUserRole(Integer userId) throws ServiceException{
        if(userId == null){throw new ServiceException("Invalid userId");}
        List<Role> roles = userDao.getUserRoleByUserId(userId,true);
        if( roles != null && roles.size() == 1 && roles.get(0) == null){roles = new ArrayList<Role>();}
        return roles;
    }

    public List<Role> getUserOweRole(Integer userId,Integer level) throws ServiceException{
        if(userId == null){throw new ServiceException("Invalid userId");}
        List<Role> roles = userDao.getUserOweRoleByUserId(userId,level);
        if( roles != null && roles.size() == 1 && roles.get(0) == null){roles = new ArrayList<Role>();}
        return roles;
    }

    @Transactional(rollbackFor = {Exception.class},propagation = Propagation.REQUIRED)
    public int addUserRoleList(Set<TSysUserRole> userRoleSet,int level) throws ServiceException,BusinessException{
        if(userRoleSet == null || userRoleSet.isEmpty()){throw new ServiceException("Invalid userRoleList! It's blank!");}
        Set<TSysRole> roles = roleDao.filterHigherLevel(this.getRoleIds(userRoleSet),level);
        if (roles == null || roles.isEmpty()
                || (roles.size() ==1 && roles.contains(null))){
            return userDao.addUserRole(userRoleSet);
        }
        throw new BusinessException("权限等级不足！配置的用户角色中有超等级的角色。");
    }

    @Transactional(rollbackFor = {Exception.class})
    public int removeUserRoleList(Set<TSysUserRole> userRoleSet,int level) throws ServiceException,BusinessException{
        if(userRoleSet == null || userRoleSet.isEmpty()){throw new ServiceException("Invalid userRoleList! It's blank!");}
        Set<TSysRole> roles = roleDao.filterHigherLevel(this.getRoleIds(userRoleSet),level);
        if (roles == null || roles.isEmpty()
                || (roles.size() ==1 && roles.contains(null))){
            return userDao.removeUserRole(userRoleSet);
        }
        throw new BusinessException("权限等级不足！删除的用户角色中有超等级的角色。");
    }

    private Set<Integer> getRoleIds(Set<TSysUserRole> userRoleSet){
        Set<Integer> result = new HashSet<Integer>();
        if (userRoleSet == null || userRoleSet.isEmpty()){
            return result;
        }

        for(TSysUserRole userRole:userRoleSet){
            if(userRole == null || userRole.getRoleId() == null){
                continue;
            }
            result.add(userRole.getRoleId());
        }

        return result;
    }

}
